package cn.tedu.tmall.front.mall.controller;

import cn.tedu.tmall.common.pojo.vo.PageData;
import cn.tedu.tmall.common.web.JsonResult;
import cn.tedu.tmall.front.mall.pojo.vo.CategoryListItemVO;
import cn.tedu.tmall.front.mall.pojo.vo.CategoryTreeItemVO;
import cn.tedu.tmall.front.mall.service.ICategoryService;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.hibernate.validator.constraints.Range;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * 处理类别相关请求的控制器类
 *
 * @author java@tedu.cn
 * @version 3.0
 */
@Slf4j
@RestController
@RequestMapping("/categories")
@Validated
@Api(tags = "1. 类别管理")
public class CategoryController {

    @Autowired
    private ICategoryService categoryService;

    public CategoryController() {
        log.debug("创建控制器类对象：CategoryController");
    }

    @GetMapping("/tree")
    @ApiOperation("查询类别树")
    @ApiOperationSupport(order = 420)
    public JsonResult listTree() {
        log.debug("开始处理【获取类别树】的请求，参数：无");
        List<CategoryTreeItemVO> categoryTree = categoryService.listTree();
        return JsonResult.ok(categoryTree);
    }

    @GetMapping("/list-by-parent")
    @ApiOperation("根据父级查询子级列表")
    @ApiOperationSupport(order = 421)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "parentId", value = "父级类别ID", required = true, paramType = "query", dataType = "long"),
            @ApiImplicitParam(name = "page", value = "页码", defaultValue = "1", paramType = "query", dataType = "int")
    })
    public JsonResult list(@Range(message = "请提交有效的父级类别ID值！") Long parentId,
                           @Range(min = 1, message = "请提交有效的页码值！") Integer page) {
        log.debug("开始处理【根据父级类别查询子级类别列表】的请求，父级类别：{}，页码：{}", parentId, page);
        Integer pageNum = page == null ? 1 : page;
        PageData<CategoryListItemVO> pageData = categoryService.listByParent(parentId, pageNum, Integer.MAX_VALUE);
        return JsonResult.ok(pageData);
    }

}
